//
//  MCBaseTableViewController.swift
//  HXWeibo
//
//  Created by HX on 15/11/23.
//  Copyright © 2015年 HX. All rights reserved.
//

import UIKit

class MCBaseTableViewController: UITableViewController
{
    //: 用户登录的标志
    var userloagin = MCUserAccount.userLogin
    
    override func viewDidLoad()
    {
        //: 如果用户已经登录，正常加载tableView,否则就创建访客视图
        userloagin ? super.loadView() : setupVistorView()
    }
    
    //: 创建访客视图
    private func setupVistorView()
    {
        view = vistorView
        
        vistorView.delegate = self
        
        // MARK: - 设置导航栏按钮
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "注册", style: UIBarButtonItemStyle.Plain, target: self, action: "vistorViewRegisterClick")
        
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "登录", style: UIBarButtonItemStyle.Plain, target: self, action: "vistorViewLoginClick")
        
        // 根据不同的控制器,设置不同的访客视图内容
        if self is MCHomeViewController
        {
            vistorView.startRotationAnimation()
            
            // MARK: - 通知 监听应用进入和退出后台
            NSNotificationCenter.defaultCenter().addObserver(self, selector: "didEnterBackground", name: UIApplicationDidEnterBackgroundNotification, object: nil)
            
            NSNotificationCenter.defaultCenter().addObserver(self, selector: "didBecomeActive", name: UIApplicationDidBecomeActiveNotification, object: nil);
        }
        else if self is MCMessageViewController
        {
            vistorView.setupVistorView("visitordiscover_image_message", message: "登录后，别人评论你的微博，发给你的消息，都会在这里收到通知")
        }
        else if self is MCDiscoverViewController
        {
            vistorView.setupVistorView("visitordiscover_image_message", message: "登录后，最新、最热微博尽在掌握，不再会与实事潮流擦肩而过")
        }
        else if self is MCProfileViewController
        {
            vistorView.setupVistorView("visitordiscover_image_profile", message: "登录后，你的微博、相册、个人资料会显示在这里，展示给别人")
        }
    }
    
    // MARK: - 移除通知
    deinit
    {
        if self is MCHomeViewController
        {
            NSNotificationCenter.defaultCenter().removeObserver(self)
        }
    }
    
    // 应用进入后台调用这个方法 实现通知的方法,不让将程序退到后台会崩溃
    func didEnterBackground()
    {
        vistorView.pauseAnimation()
    }
    
    // 进入应用程序调用这个方法
    func didBecomeActive()
    {
        vistorView.resumeAnimation()
    }
    
    // MAKE: - 懒加载访客视图
    private lazy var vistorView: MCVistorView = MCVistorView()
}

// MARK: - 扩展
extension MCBaseTableViewController: MCVistorViewDelegate
{
    // MARK: - 导航栏注册按钮的点击
    func vistorViewRegisterClick()
    {
        print("导航栏注册按钮的点击")
    }
    
    // MARK: - 导航栏登录按钮的点击
    func vistorViewLoginClick()
    {
        let oauthVC = MCOAuthViewController()
        
        presentViewController(UINavigationController(rootViewController: oauthVC), animated: true, completion: nil)
        
    }
}






























